#include "matrixfunctions.h" 
#include <ctime>
#include <stdlib.h>

bool ismelt(int mat[][100], int row, int col)
{
	int i, j;
	for(i=0; i<row; ++i)
		for(j=0; j<col; ++j)
			if(mat[i][j]==1)
				return false;
return true;
}

int main()
{
	int mat[100][100], tmp[100][100];
	int row, col, i, j, a, b, time=0, count;
	cout<<"Number of rows : ";
	cin>>row;
	cout<<"Number of columns : ";
	cin>>col;
	readmatrix(mat, row, col);
	system("clear");
	printmatrix(mat, row, col);
	for(i=0; i<row; ++i)
			for(j=0; j<col; ++j)
				tmp[i][j]=mat[i][j];
	while(!ismelt(mat, row, col)){
		for(i=0; i<row; ++i)
			for(j=0; j<col; ++j){
				count=0;
				if(mat[i][j]==1){
					for(a=-1; a<=1; a++)
						for(b=-1; b<=1; b++){
							if(a!=0 || b!=0)
								if(i+a>=0 && i+a<row && j+b>=0 && j+b<col)
									if(mat[i+a][j+b]==0)
										++count;
		
						}
					if(count>2)
						tmp[i][j]=0;			
				}
			}
		++time;
		matrixcopy(mat, tmp, row, col);
		printmatrix(mat, row, col);
		sleep(1);
		system("clear");
	}
	cout<<time<<" time units"<<endl;
	system("pause");
return 0;
}
